/*
	Copyright (c) 2009, SpLib.net
  	All rights reserved.
  	
  	Author:6spring
  	About:
*/

package SpLib.std
{
	public class utils
	{
		public function utils()
		{
		}
		
		public static function Str2Key(str:String):uint
		{
			var rs:uint = 0
			for(var i:int=0;i<str.length;i++)
			{
				var code:Number= str.charCodeAt(i)
				var tmp:uint = 0
				tmp = code & 0x000000ff
				rs *= 16777619
				rs ^= uint(tmp)
				rs *= 16777619
				tmp = (code&0x0000ff00)>>8
				rs ^= uint(tmp)						
			}				
			return rs
		}
		
		public static function GetKey_Hash(key:*):uint
		{
			var hash:uint = 0;
			var keytype:String = typeof(key);
			if(keytype=="number")
			{
				hash = key as uint;
			}
			
			if(hash!=0)
			{
				return hash;
			}
			
			if(keytype=="string")
			{
				hash = Str2Key(key as String);
			}
			else
			{
				if(keytype=="number" || keytype=="object")
				{
					hash = Str2Key(key.toString());
				}
			}
			
			return hash;
		}
		
		public static function strcmp(str1:String,str2:String):int
		{
			if(str1==str2)
			{
				return 0;
			}
			
			var i:int;
			var c1:Number;
			var c2:Number;
			if(str1.length<=str2.length)
			{
				for(i=0;i<str1.length;i++)
				{
					c1 = str1.charCodeAt(i);
					c2 = str2.charCodeAt(i)
					
					if(c1==c2)
					{
						continue;
					}
					
					return c1>c2?1:-1;
				}
				
				return -1;
			}
			
			for(i=0;i<str2.length;i++)
			{
				c1 = str1.charCodeAt(i);
				c2 = str2.charCodeAt(i)
				
				if(c1==c2)
				{
					continue;
				}
					
				return c1>c2?1:-1;
			}
				
			return 1;
		}
	}
}